<?php  
	namespace Home\Controller;
	use Think\Controller;
    //登录注册管理
    //@author  程传民
	class LoginController extends Controller{
        //检查是否有该用的方法
        public function _empty(){
            header("HTTP/1.0 404 Not Found");
            $this -> display("Empty/empty");
        }
        
		//跳转到登录界面
		public function index(){
            unset($_SESSION);
			$this->display("User/login");
		}

		//判断此用户是否被禁用
        public function find(){
            $user = D("users");
            $arr = array("name"=>I("get.name"));
            $info = $user->where($arr)->find();
            if(!$info){
                echo "用户不存在";
            }else if($info['can_login']==0){
                echo "禁用";
            }else{
                echo "通过";
            }
        }

        public function login(){
			$name = I("post.name");
			$password = md5(I("post.password"));
			$mod = D("users");
			$arr = array("name"=>$name,"password"=>$password);
			$info = $mod->where($arr)->find();
			
			if($info){
				$_SESSION['name'] = $info['name'];
                $_SESSION['userid'] = $info['id'];
                $_SESSION['role_id'] = $info['role_id'];
                $_SESSION["class"] = $info['class'];
                $_SESSION['pic'] = $info['pic'];

				$this->success("登录成功",U("User/pub"));  

			}else{
				$this->error("用户名不存在或密码错误");
			}
		}

		//退出登录
        public function outLogin(){
            //退出登录  删除所有的session值
            unset($_SESSION);
            session_destroy();
            $this->success("退出成功",U("User/pub"));
        }

        //加载忘记密码页面
        public function forget(){
            unset($_SESSION['param']);
        	$this->display("User/forget");
        }

        //获取短信验证码
        public function getyzm(){

        	$phone = I("post.phone");
        	$param = "";
        	for($i=0;$i<4;$i++){
        		$param .= ceil(rand(0,9));
        	}
            $_SESSION['param'] = $param;
            $_SESSION['phone'] = $phone;
        	test($phone,$param);
        }
		
		// 验证码定时失效
        public function invalid(){
            // session('param','[destroy]');
            $_SESSION['param'] = rand(10000,100000);
        }
		
        //验证输入的验证码是否正确
        public function contrast(){

            if($_SESSION['param'] == I("post.yzms")){
                echo "成功";
            }else{
                echo "失败";
            }

        }

        //加载修改密码页面
        public function addreset(){
            $this->assign("phone",$_SESSION['phone']);
            $this->display("User/resetpass");
        }

        //修改密码
        public function resetpass(){
            $arr = array("phone"=>$_SESSION['phone']);
            $arrs = array("password"=>md5(I("post.password")));
            $users = D("users");
            $info = $users->where($arr)->save($arrs);
            if($info){
                echo "成功";
            }else{
                echo "失败";
            }
        }

		//跳转到注册界面
		public function toReg(){
			$mod = D("class");
			$list = $mod->select();
			//$arr数组存放班级
			$class = array();
			foreach($list as $key=>$value){
				if($value['status']<=1){
					$class[] = array("id"=>$value['id'],"name"=>$value['name']);
				}
			}

			$this->assign("class",$class);
			$this->display("User/register");
		}

		//添加用户(注册处理)
		public function add(){

			$mod = D("users");

            $data['name'] = I("post.name");
            $data['password'] = md5(I("post.password"));
            $data['can_login'] = 1;
			$data['role_id'] = 11;
			$data['team'] = 0;
			$data['class'] = I("post.class");
			$data['sex'] = I("post.sex");
			$data['card'] = I("post.card");
			$data['address'] = I("post.address");
			$data['phone'] = I("post.phone");
			$data['qq'] = I("post.QQ");
			$data['email'] = I("post.email");
			$data['school_address'] = I("post.school_address");
			$data['school'] = I("post.school");
			$data['education'] = I("post.education");
			$data['subject'] = I("post.subject");
			$data['computer'] = I("post.computer");
			$data['work_year'] = I("post.work_year");
			$data['target'] = I("post.target");
			$data['contacts_name'] = I("post.contacts_name");
			$data['contacts_relation'] = I("post.contacts_relation");
			$data['contacts_phone'] = I("post.contacts_phone");

            $info = $mod->add($data);

            if($info){
                $this->success("注册成功",U("Login/index"));
            }

		}


		//开启验证码   登录页面
        public function verify(){
            $config = array(
                    'fontSize' => 20,       //验证码大小
                    'length'   => 4,        //验证码位数
                    // 'expire'   => 10,        //验证码的有效期  单位秒
                    'useImgBg' => true,     //是否使用背景图片 默认为false
                    'useCurve' => true,     //是否使用混淆曲线  默认为true
                    'useNoise' => true,     //是否添加杂点
                    'codeSet'  => '1234567890',  //验证码字符集
                    'imageW'   => 185,
                    'imageH'   => 40
                );
            $Verify = new \Think\Verify($config);
            $Verify->entry();
        }

        public function yzm(){
            if(IS_POST){
                $Verify = new \Think\Verify();
                $info = $Verify->check($_POST['verifys'],$id='');
                if($info){
                    echo "1";
                }else{
                    echo "0";
                }
                
            }else{

                $this->display("Stu/yzm");
            }
        }

        //Ajax验证
		public function preg_password(){
			if(IS_AJAX){
			    $password = I("post.password");
			    if (!preg_match("/^[a-zA-Z\d]{6,18}$/",$password)) {
			    	echo "密码格式不正确";
			    }else{
			    	echo '正确';
			    }
		    }
		}

        //注册  身份证号验证
        public function preg_card(){
            $arr = array("card"=>I("post.card"));
            $users = D("users");

            $info = $users->where($arr)->find();
            if($info){
                echo "1";
            }else{
                echo "0";
            }
        }

		public function preg_repassword(){
			if(IS_AJAX){
			    $password = I("post.password");
			    $repassword = I("post.repassword");
			    if ($password === $repassword) {
			    	echo '正确';
			    }else{
			    	echo "两次输入密码不一致";
			    }
		    }
		}


		//手机号验证
		public function preg_phone(){
			 $arr = array("phone"=>I("post.phone"));
             $users = D("users");
			 $info = $users->where($arr)->find();
		    if($info){
                echo "1";
            }else{
                echo "0";
            }
		}

	}
?>